<?php

namespace App\Http\Controllers\Plat;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;
use function GuzzleHttp\json_decode;
use \GatewayWorker\Lib\Gateway;

use  App\Models\User;
use  App\Models\Weixin;
use  App\Models\WeixinFriendList;
use App\Models\WeixinGroup;

class WeixinController extends Controller
{
    public function __construct(Request $request)
    {
        $token = $request->header('authorization');
        $key = explode('.',$token);
        $user = Redis::get($key[0]);
        $user_data = json_decode($user);
        $this->user_id = $user_data->id;
    } 

    public function weixinList(Request $request)
    {
        $user_data['id'] = $this->user_id;
        $weixin_ids = WeixinFriendList::where('user_id',$user_data['id'])->pluck('weixin_uid');
        $data = Weixin::whereIn('uid',$weixin_ids)->select('uid','nickname','small_head_url','phone_client_id')->get();
        foreach ($data as $key => $value) {
            if($value['phone_client_id']){
                $data[$key]['online'] = Gateway::isOnline($value['phone_client_id']);
            }
            $data[$key]['online'] = 0 ;
        }
        return $this->returnOk($data);
    }

    public function weixinFriendList(Request $request)
    {
        $input = Input::only('uid');
        $validator = Validator::make($input, [        
            'uid' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $user_data['id'] = $this->user_id;
        $data = WeixinFriendList::where('user_id',$user_data['id'])->where('weixin_uid',$input['uid'])
            ->select('nickname','small_head_url','uid','com_remark','province','city')
            ->orderBy('updated_at','desc')
            ->get();
        return $this->returnOk($data);
    }

    public function weixinFriendGroup(Request $request)
    {
        $input = Input::only('uid');
        $validator = Validator::make($input, [        
            'uid' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $user_data['id'] = $this->user_id;
        $friend = WeixinFriendList::where('user_id',$user_data['id'])->where('weixin_uid',$input['uid'])
            ->select('id','nickname','small_head_url','uid','com_remark','province','city','weixin_group_id')->get();
        $group_ids = WeixinFriendList::where('user_id',$user_data['id'])->pluck('weixin_group_id');
        $group = WeixinGroup::whereIn('id',$group_ids)->select('name','id')->get();
        $data = [];
        foreach ($friend as $key => $value) {
            if($value['weixin_group_id']){
                foreach ($group as $k => $val) {
                    $data[$k]['name'] = $val['name'];
                    $data[$k]['id'] = $val['id'];
                    if($value['weixin_group_id']==$val['id']){
                        $flag = 0;
                        $data[$k]['data'][] = $value;
                    }
                }
            }else{
                $data[count($group)]['id'] = 0;
                $data[count($group)]['name'] = '未分组';
                $data[count($group)]['data'][] = $value;
            }
        }

        return $this->returnOk(array_values($data));
    }
}